<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :keyboard="false"
    :forceRender="true"
    fullscreen
    switchFullscreen
    @cancel="handleCancel"
    :id="prefixNo"
    style="top: 20px; height: 95%"
  >
    <template slot="footer">
      <a-button @click="handleCancel">取消</a-button>
      <a-button v-if="checkFlag && isCanCheck" :loading="confirmLoading" @click="handleOkAndCheck">保存并审核</a-button>
      <a-button type="primary" :loading="confirmLoading" @click="handleOk">保存（Ctrl+S）</a-button>
      <!--发起多级审核-->
      <a-button v-if="!checkFlag" @click="handleWorkflow()" type="primary">提交流程</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row class="form-row" :gutter="24">
          <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户">
              <a-select
                placeholder="请选择客户"
                v-decorator="['customerId', validatorRules.customerId]"
                :dropdownMatchSelectWidth="false"
                showSearch
                optionFilterProp="children"
              >
                <a-select-option v-for="(item, index) in cusList" :key="index" :value="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="单据日期">
              <j-date v-decorator="['billTime', validatorRules.billTime]" />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="单据编号">
              <a-input placeholder="请输入单据编号" v-decorator.trim="['orderNo']" />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="财务人员">
              <a-select
                placeholder="请选择财务人员"
                v-decorator="['salespersonId']"
                :dropdownMatchSelectWidth="false"
                showSearch
                optionFilterProp="children"
              >
                <a-select-option v-for="(item, index) in userList" :key="index" :value="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <j-editable-table
          :ref="refKeys[0]"
          :loading="accountTable.loading"
          :columns="accountTable.columns"
          :dataSource="accountTable.dataSource"
          :minWidth="minWidth"
          :maxHeight="300"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"
          @added="onAdded"
          @valueChange="onValueChange"
        >
          <template #inOutItemAdd>
            <a-divider v-if="quickBtn.inOutItem" style="margin: 4px 0" />
            <div v-if="quickBtn.inOutItem" style="padding: 4px 8px; cursor: pointer" @click="addInOutItem('in')">
              <a-icon type="plus" /> 新增收支项目
            </div>
          </template>
        </j-editable-table>
        <a-row class="form-row" :gutter="24">
          <a-col :lg="24" :md="24" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="{ xs: { span: 24 }, sm: { span: 24 } }" label="">
              <a-textarea :rows="2" placeholder="请输入备注" v-decorator="['remark']" style="margin-top: 8px" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="收入账户">
              <a-select
                placeholder="请选择收入账户"
                v-decorator="['settlementAccountId', validatorRules.settlementAccountId]"
                :dropdownMatchSelectWidth="false"
                showSearch
                optionFilterProp="children"
              >
                <a-select-option v-for="(item, index) in accountList" :key="index" :value="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="收入金额">
              <a-input
                placeholder="请输入收入金额"
                v-decorator.trim="['amountReceived', validatorRules.amountReceived]"
              />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24"> </a-col>
          <a-col :lg="6" :md="12" :sm="24"> </a-col>
        </a-row>
      </a-form>
    </a-spin>
    <account-modal ref="accountModalForm" @ok="accountModalFormOk"></account-modal>
    <person-modal ref="personModalForm" @ok="personModalFormOk"></person-modal>
    <workflow-iframe ref="modalWorkflow" @ok="workflowModalFormOk"></workflow-iframe>
    <in-out-item-modal ref="inOutItemModalForm" @ok="inOutItemModalFormOk('in')"></in-out-item-modal>
  </j-modal>
</template>
<script>
import pick from 'lodash.pick'
import AccountModal from '../../system/modules/AccountModal'
import PersonModal from '../../system/modules/PersonModal'
import WorkflowIframe from '@/components/tools/WorkflowIframe'
import InOutItemModal from '../../system/modules/InOutItemModal'
import { FormTypes } from '@/utils/JEditableTableUtil'
import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
import { FinancialModalMixin } from '../mixins/FinancialModalMixin'
import JUpload from '@/components/jeecg/JUpload'
import JDate from '@/components/jeecg/JDate'
export default {
  name: 'ItemInModal',
  mixins: [JEditableTableMixin, FinancialModalMixin],
  components: {
    AccountModal,
    PersonModal,
    WorkflowIframe,
    InOutItemModal,
    JUpload,
    JDate,
    VNodes: {
      functional: true,
      render: (h, ctx) => ctx.props.vnodes,
    },
  },
  data() {
    return {
      title: '操作',
      width: '1600px',
      moreStatus: false,
      // 新增时子表默认添加几行空数据
      addDefaultRowNum: 1,
      visible: false,
      prefixNo: 'otherIncome',
      model: {},
      fileList: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      refKeys: ['accountDataTable'],
      activeKey: 'accountDataTable',
      accountTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '收入类别',
            key: 'incomeCategory',
            width: '20%',
            type: FormTypes.select,
            placeholder: '请选择${title}',
            options: [],
            allowSearch: true,
            validateRules: [{ required: true, message: '${title}不能为空' }],
          },
          {
            title: '金额',
            key: 'amount',
            width: '10%',
            type: FormTypes.inputNumber,
            statistics: true,
            placeholder: '请选择${title}',
            validateRules: [{ required: true, message: '${title}不能为空' }],
          },
          { title: '备注', key: 'remark', width: '30%', type: FormTypes.input, placeholder: '请选择${title}' },
        ],
      },
      confirmLoading: false,
      validatorRules: {
        customerId: {
          rules: [{ required: true, message: '请选择客户!' }],
        },
        billTime: {
          rules: [{ required: true, message: '请选择单据日期!' }],
        },
        settlementAccountId: {
          rules: [{ required: true, message: '请选择收入账户!' }],
        },
        amountReceived: {
          rules: [{ required: true, message: '请输入收入金额!' }],
        },
      },
      url: {
        add: '/otherIncome',
        edit: '/otherIncome/update',
        detailList: '/accountItem/getDetailList',
      },
    }
  },
  created() {},
  methods: {
    //调用完edit()方法之后会自动调用此方法
    editAfter() {
      this.billStatus = '0'
      if (this.action === 'add') {
        this.addInit(this.prefixNo)
        this.fileList = []
      } else {
        this.model.billTime = this.model.orderDate
        this.$nextTick(() => {
          this.form.setFieldsValue(
            pick(
              this.model,
              'billTime',
              'orderNo',
              'customerId',
              'customerName',
              'salespersonId',
              'settlementAccountId',
              'remark',
              'receiptAmount',
              'amountReceived'
            )
          )
        })
        this.accountTable.dataSource = this.model.incomeDetailVOList
      }
      this.initAccount()
      this.initInbcomeBusinessType()
      this.initCustomer()
      this.initUser()
    },
    //提交单据时整理成formData
    classifyIntoFormData(allValues) {
      let billMain = Object.assign(this.model, allValues.formValue)
      let detailArr = allValues.tablesValue[0].values

      if (this.model.id) {
        billMain.id = this.model.id
      }
      billMain.incomeDetailDTOList = detailArr
      billMain.orderDate = billMain.billTime
      return billMain
    },
    //改变本次欠款的值
    autoChangeAmount(target) {
      let allEachAmount = target.statisticsColumns.eachAmount - 0
      this.$nextTick(() => {
        this.form.setFieldsValue({ changeAmount: allEachAmount })
      })
    },
  },
}
</script>
<style scoped>
</style>